package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.Distributionmode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.condition.DistributionmodeCondition;
import cn.b.sky.mes.mm.service.DistributionmodeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/distributionmode/*")
public class DistributionmodeController extends SecurityController{
	
	 @Resource
	 private DistributionmodeService  distributionmodeService ;
	
	  @RequestMapping(value = "distributionmodeList")
	  public String distributionmodeList(Model model, @ModelAttribute("condition") DistributionmodeCondition distributionmodeCondition ,
	      String navTabId) {
		  distributionmodeCondition.setSiteCode(getSiteCode());
	    List<Distributionmode> distributionmodes = distributionmodeService.selectdistributionmodeList(distributionmodeCondition);
	    int count = distributionmodeService.getDistributionmodeCount(distributionmodeCondition);
	    distributionmodeCondition.setData(distributionmodes);
	    distributionmodeCondition.setTotalCount(count);
	    model.addAttribute("page", distributionmodeCondition);
	    model.addAttribute(NAVTABID, navTabId);
	    return "cm/distributionmode/distributionmode_list";
	  }

	  @RequestMapping(value = "addDistributionmode")
	  public String addDistributionmode(String navTabId, Model model) {
	    model.addAttribute(NAVTABID, navTabId);
	    model.addAttribute("pattern", "add");
	    return "cm/distributionmode/distributionmode_add";
	  }
	  
	  @RequestMapping(value = "modifyDistributionmode")
	  public String modifyDistributionmode(String navTabId, String id, Model model) {
		  Distributionmode distributionmode = distributionmodeService.getDistributionmode(this.getSiteCode(),id);
	    model.addAttribute(NAVTABID, navTabId);
	    model.addAttribute("distributionmode", distributionmode);
	    model.addAttribute("pattern", "modify");
	    return "cm/distributionmode/distributionmode_add";
	  }
	  

	  @RequestMapping(value = "viewDistributionmode")
	  public String viewMaterialType(String navTabId, String id, Model model) {

		Distributionmode distributionmode = distributionmodeService.getDistributionmode(this.getSiteCode(),id);
	    model.addAttribute(NAVTABID, navTabId);
	    model.addAttribute("distributionmode", distributionmode);
	    model.addAttribute("pattern", "read");
	    return "cm/distributionmode/distributionmode_add";

	  }
	  @RequestMapping(value = "validateDistributionModeCode")
	  @ResponseBody
	  public boolean validateDistributionModeCode(String distributionModeCode, Model model) {
	    if (!StringUtils.isBlank(distributionModeCode)) {
	    	Distributionmode distributionmode = distributionmodeService.getDistributionmode(this.getSiteCode(),distributionModeCode.toUpperCase());
	      return distributionmode == null;
	    }
	    return true;
	  }
	  
	  @RequestMapping(value = "saveDistributionmode")
	  @ResponseBody
	  public DWZResult saveMaterialType(Model model, String navTabId, String id, Distributionmode distributionmode,
	      HttpServletRequest request) {
	    if (distributionmode == null) {
	      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
	    }
	    setCreateInfo(distributionmode);
	    distributionmode.setSiteCode(distributionmode.getSiteCode());
	    if (StringUtils.isBlank(id)) {
	    	distributionmodeService.insertDistributionmode(distributionmode);
	      String forwardUrl = getForwardUrl(request) + "/distributionmode/distributionmodeList?navTabId=" + navTabId;
	      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, forwardUrl, null);
	    } else {
	    	distributionmodeService.updateDistributionmode(distributionmode);
	      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
	    }
}
	  
	  @RequestMapping("deleteDistributionmode")
	  @ResponseBody
	  public DWZResult deleteDistributionmode(Model model, String navTabId, String codes, HttpServletRequest request) {

		  String[] idArr = codes.split(",");
			List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
			int failures = 0;
			for (int i = 0; i < idArr.length; i++) {
				String code = idArr[i];
				String message = null;
				message = distributionmodeService.deleteDistributionmode(
						this.getSiteCode(), code);
				boolean flag = true;
				if (!StringUtils.isEmpty(message)) {
					failures += 1;
					flag = false;
					message = getImeViewMessageI18n(
							SecurityContextUtil.getLanguageCode(), message);
				}
				BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
				reports.add(report);
			}
			if (failures == 0) {
				return navTabAjaxDone(
						navTabId,
						null,
						null,
						null,
						getImeViewMessageI18n(this.getCurrentLanguage(),
								"successfulOperation.M"), null);
			} else {
				return batchjobAjaxDone(request, reports);
			}
		}
}